use std::slice;



fn main() {
    println!("Hello, world!");
    rawpointser();
    unsafe_func();
}

fn rawpointser() {
    let x = 5;
let p = &x as *const i32;
let y = unsafe { *p };
let mut num  = 5;
let r1 = &num as *const i32;// 不可变引用
let r2 = &mut num as *mut i32; // 可变引用
//  使用裸指针访问变量，需要使用unsafe关键字
unsafe {
    println!("r1 is: {}", *r1);
    println!("r2 is: {}", *r2);
}
}

fn unsafe_func() {
    let mut v = vec![1, 2, 3, 4, 5];
let r = &mut v[..];

let (a, b) = r.split_at_mut(3);

assert_eq!(a, &mut [1, 2, 3]);
assert_eq!(b, &mut [4, 5]);
}


fn split_at_mut(slice: &mut [i32], mid: usize) -> (&mut [i32], &mut [i32]) {
    let len = slice.len();
    let ptr = slice.as_mut_ptr();
    assert!(mid <= len);
    unsafe {
        (slice::from_raw_parts_mut(ptr, mid), slice::from_raw_parts_mut(ptr.add(mid), len - mid))
    }
}